﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ServiceGUI
{
    public partial class UserViewControl : UserControl
    {
        private string _username;
        private WorkBase.ServerClient _client;


        public delegate void Logout();
        private Logout _logout;

        public UserViewControl(string username, WorkBase.ServerClient client, Logout logout)
        {
            InitializeComponent();

            _username = username;
            _client = client;
            _logout = logout;

            UserName.Text = string.Format("Hello {0}", _username);

            _client.GetUsersCompleted += new EventHandler<WorkBase.GetUsersCompletedEventArgs>(_client_GetUsersCompleted);
            _client.GetUsersAsync();
        }

        void _client_GetUsersCompleted(object sender, WorkBase.GetUsersCompletedEventArgs e)
        {
            var user = (from u in e.Result where u.Name == _username select u).FirstOrDefault();

            if (user != null)
            {
                UserPost.Text = string.Format("Your post in company: {0}", user.Post);
                UserSalary.Text = string.Format("Your salary: {0}", user.Salary);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _logout();
        }
    }
}
